﻿@{
    ViewBag.Title = "GrammarForIn";
    Layout = "~/Views/Shared/_Page.cshtml";
}

<h2>Grammar ForIn Loop</h2>
<br />
<h3>Path: @ViewBag.Path</h3>
<br />
<h3>See the result on console</h3>
<br />

<p>
    for-in loops should be used to iterate over nonarray objects. Looping with for-in is
    also called enumeration.
    Technically, you can also use for-in to loop over arrays (because in JavaScript arrays
    are objects), but it’s not recommended. It may lead to logical errors if the array object
    has already been augmented with custom functionality. Additionally, the order (the
    sequence) of listing the properties is not guaranteed in a for-in. So it’s preferable to
    use normal for loops with arrays and for-in loops for objects.
</p>

<script type="text/javascript">
    window.onload = function() {
        // the object
        var man = {
            hands: 2,
            legs: 2,
            heads: 1
        };

        // somewhere else in the code
        // a method was added to all objects
        if (typeof Object.prototype.clone === "undefined") {
            Object.prototype.clone = function () { };
        }

        // for-in loop
        ZEUS.DEBUG.log("Print all properties of man:");
        ZEUS.DEBUG.log("----------------------------------");
        for (var i in man) {
            if (man.hasOwnProperty(i)) { // filter
                ZEUS.DEBUG.log(i + ":" + man[i]);
            }
        }
    };
</script>
